package impl.repository;

import interfaces.repository.ShippingRepository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import sun.awt.shell.ShellFolder;
import dao.AccessDatabase;
import domain.Customer;
import domain.Shipping;

public class StubShippingRepositoryImpl implements ShippingRepository {

	@Override
	public Shipping findByID(int idCustomer) {

		StubAddresRepositoryImpl sari = new StubAddresRepositoryImpl();
		
		Shipping shipping = null;
		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st
					.executeQuery("SELECT * FROM billing WHERE idCustomer='"
							+ idCustomer + "'");

			shipping = new Shipping(rs.getInt("idShipping"), rs.getString("firstname"), rs.getString("lastname"), sari.findById(rs.getInt("idAddress")));

			con.close();

			return shipping;
		} catch (SQLException e) {
			return null;
		}

	}

	@Override
	public boolean edit(Shipping before, Shipping after) {

		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("UPDATE `shipping` SET `firstname`='" + after.getFirstName()
					+ "', `lastname`='" + after.getLastName()
					+ "' WHERE `idBilling`='" + before.getId() + "'");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}

	}

	@Override
	public boolean add(Shipping shipping) {

		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("INSERT INTO `shipping` (`idAddress`,`firstname`,`lastname`) VALUES ('"
					+ shipping.getAddress().getId()
					+ "','"
					+ shipping.getFirstName()
					+ "','"
					+ shipping.getLastName()
					+ "');");
			con.close();
			return true;
		} catch (SQLException e) {
			System.out
					.println("nao foi possivel prosseguir com o registro" + e);
			return false;
		}
	}

	@Override
	public boolean delete(Shipping shipping) {
		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("DELETE FROM `shipping` WHERE `idShipping`='"
					+ shipping.getId() + "'");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}
	}

}
